package com.china.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.china.dao.BaseDaoI;

@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDaoI<T> {
	
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Serializable save(T t) {
		// TODO Auto-generated method stub
		return this.sessionFactory.getCurrentSession().save(t);
	}

	@Override
	public void delete(T t) {
		// TODO Auto-generated method stub
		this.sessionFactory.getCurrentSession().delete(t);
	}

	@Override
	public void update(T t) {
		// TODO Auto-generated method stub
		this.sessionFactory.getCurrentSession().update(t);
	}

	@Override
	public void saveOrUpdate(T t) {
		// TODO Auto-generated method stub
		this.sessionFactory.getCurrentSession().saveOrUpdate(t);
	}

	@Override
	public T get(Class<T> t, Serializable id) {
		// TODO Auto-generated method stub
		return (T) this.sessionFactory.getCurrentSession().get(t, id);
	}

	@Override
	public T get(String hql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		List<T> t = this.find(hql,params);
		if(t != null && t.size()>0){
			return t.get(0);
		}
		return null;
	}

	@Override
	public List<T> find(String hql) {
		// TODO Auto-generated method stub
		return this.sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	@Override
	public List<T> find(String hql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		Query q = this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params != null && params.size()>0 ){
			for(String key : params.keySet()){
				q.setParameter(key, params.get(key));
			}
		}
		
		return q.list();
	}

	@Override
	public List<T> find(String hql, Map<String, Object> params, Integer page, Integer rows) {
		// TODO Auto-generated method stub
		Query q = this.sessionFactory.getCurrentSession().createQuery(hql);
		if(page == null || page<1){
			page = 1;
		}
		if(rows == null || rows<1){
			rows = 10;
		}
		if(params != null && params.size() >0){
			for(String key : params.keySet()){
				q.setParameter(key, params.get(key));
			}
		}
		return q.setFirstResult((page-1)*rows).setMaxResults(rows).list();
	}

	@Override
	public Long count(String hql) {
		// TODO Auto-generated method stub
		return (Long) this.sessionFactory.getCurrentSession().createQuery(hql).uniqueResult();
	}

	@Override
	public Long count(String hql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		Query q = this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params != null && params.size() > 0){
			for(String key : params.keySet()){
				q.setParameter(key, params.get(key));
			}
		}
		return (Long) q.uniqueResult();
	}

	@Override
	public Integer executeHql(String hql) {
		// TODO Auto-generated method stub
		return this.sessionFactory.getCurrentSession().createQuery(hql).executeUpdate();
	}

	@Override
	public Integer executeHql(String hql, Map<String, Object> params) {
		// TODO Auto-generated method stub
		Query q = this.sessionFactory.getCurrentSession().createQuery(hql);
		if(params != null && params.size() > 0){
			for(String key : params.keySet()){
				q.setParameter(key,params.get(key));
			}
		}
		return q.executeUpdate();
	}

}
